package com.chenyue.cm.api.msg;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chenyue.cm.auth.Login;
import com.chenyue.cm.auth.LoginUser;
import com.chenyue.cm.bean.Success;
import com.chenyue.cm.common.vo.Page;
import com.chenyue.cm.live.domain.JdLive;
import com.chenyue.cm.live.service.JdLiveService;
import com.chenyue.cm.msg.domain.JdMsg;
import com.chenyue.cm.msg.service.JdMsgService;
import com.chenyue.cm.push.jpush.PushUtils;
import com.chenyue.cm.push.jpush.vo.NotifyType;
import com.chenyue.cm.push.jpush.vo.PushContent;
import com.chenyue.cm.push.jpush.vo.PushParam;
import com.chenyue.cm.user.domain.JdAppUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Calendar;
import java.util.List;

/**
 * @author libingxing
 */
@Api(tags = "消息通知")
@RestController
@RequestMapping("/api/msg")
public class ApiMsgController {

    private final JdMsgService msgService;

    private final JdLiveService jdLiveService;

    public ApiMsgController(JdMsgService msgService, JdLiveService jdLiveService) {
        this.msgService = msgService;
        this.jdLiveService = jdLiveService;
    }

    @Login
    @GetMapping("/msgTypeList")
    @ApiOperation("消息类型列表")
    public Success<List<JdMsg>> msgTypeList(
            @LoginUser @ApiIgnore JdAppUser user
    ) {
        final LambdaQueryWrapper<JdMsg> eq = new QueryWrapper<JdMsg>().lambda()
                .eq(JdMsg::getUserId, user.getId())
                .or(msg -> msg.isNull(JdMsg::getUserId))
                .groupBy(JdMsg::getType)
                .orderByDesc(JdMsg::getCt);
        List<JdMsg> list = msgService.list(eq);
        return Success.success(list);
    }

    @Login
    @GetMapping("/msgList")
    @ApiOperation("消息列表")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "页数", name = "page", dataType = "integer", required = true),
            @ApiImplicitParam(value = "页码", name = "rows", dataType = "integer", required = true),
//            @ApiImplicitParam(value = "消息类型", name = "type", dataType = "integer", required = true)
    })
    public Success<Page<List<JdMsg>>> msgList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer rows,
            @LoginUser @ApiIgnore JdAppUser user
//            Integer type
    ) {
        final LambdaQueryWrapper<JdMsg> eq = new QueryWrapper<JdMsg>().lambda()
                .eq(JdMsg::getUserId, user.getId())
                .or().eq(JdMsg::getType, 1)
                .orderByDesc(JdMsg::getCt);
        Integer total = msgService.count(eq);
        final Page<List<JdMsg>> result = new Page<>(page, rows, total);
        eq.last(result.convertPage());
        List<JdMsg> list = msgService.list(eq);
        result.setContent(list);
        return Success.success(result);
    }

    @PostMapping("/anchorInvite")
    @Transactional(rollbackFor = Exception.class)
    public Success<String> anchorInvite(
            Integer userId
    ) {
        final JdMsg one = msgService.lambdaQuery().eq(JdMsg::getTransType, NotifyType.ANCHOR_INVITE.getCode())
                .eq(JdMsg::getUserId, userId).one();

        if (one != null) {
            return Success.error("请勿重复邀请");
        }
        //校验该用户已经是主播啦
        final JdLive jdLive = jdLiveService.lambdaQuery().eq(JdLive::getUserId, userId).last(" limit 1 ").one();
        if (jdLive != null) {
            return Success.error("该用户已经是主播啦");
        }
        JdMsg msg = new JdMsg();
        msg.setContent(NotifyType.ANCHOR_INVITE.getMsg());
        msg.setCt(Calendar.getInstance().getTime());
        msg.setTransType(NotifyType.ANCHOR_INVITE.getCode());
        msg.setType(1);
        msg.setTransParam("");
        msg.setUserId(userId);
        msgService.save(msg);

        PushContent content = new PushContent();
        content.setContent(msg.getContent());
        content.setParam("");
        PushParam param = new PushParam(userId, content, NotifyType.ANCHOR_INVITE.getCode());
        PushUtils.push(param);
        return Success.success();
    }

}
